import { _decorator, Component, EventTouch, instantiate, Node } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { GAME_POP, GAME_PREFAB, SOUND_TYPE } from '../../Data/GlobalData';
import { BallSkinInfo } from '../../Data/GlobaInterface';
import { PopSingleBallSkin } from './PopSingleBallSkin';
const { ccclass, property } = _decorator;

@ccclass('PopSkin')
/**
 * 顶部皮肤层
 */
export class PopSkin extends BaseComponent {

    @property(Node)
    private SkinNode: Node = null;

    //皮肤数组
    private SkinArr: Node[] = [];

    //本地缓存Key
    private SkinKey: string = "SkinKey";

    //皮肤信息
    private SkinInfo: BallSkinInfo[] = [];

    //默认
    private DefaultSkin: BallSkinInfo[] = [
        { Index: 0, Price: 0, IsBuy: true },
        { Index: 1, Price: 100, IsBuy: false },
        { Index: 2, Price: 300, IsBuy: false },
        { Index: 3, Price: 500, IsBuy: false },
        { Index: 4, Price: 1000, IsBuy: false }
    ];

    protected Init(): void {
        //判断是否有本地皮肤信息
        if (app.dataManager.GetStorage(this.SkinKey)) {
            this.SkinInfo = app.dataManager.GetStorage(this.SkinKey) as BallSkinInfo[];
        } else {
            this.SkinInfo = this.DefaultSkin;
            app.dataManager.SetStorage(this.SkinKey, this.SkinInfo);
        }

        for (let si of this.SkinInfo) {
            let psbs: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.PopSingleBallSkin]);
            psbs.getComponent(PopSingleBallSkin).UpdateBallInfo(si);
            psbs.parent = this.SkinNode;
            this.SkinArr.push(psbs);
        }

    }

    protected OnShow(): void {
        app.logManager.debug("显示皮肤")

        //选择显示
        for (let si of this.SkinArr) {
            si.getComponent(PopSingleBallSkin).RightIcon.active = (si.getComponent(PopSingleBallSkin).Info.Index == app.gameData.GameData.basket)
        }


    }

    /**
     * 点击关闭
     */
    private CloseClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        app.popManager.HidePop(GAME_POP.PopSkin);
    }

    /**
     * 点击皮肤
     */
    private SkinClick(_e: EventTouch) {
        let skin: Node = _e.currentTarget;

        if (app.gameData.GameData.coin < skin.getComponent(PopSingleBallSkin).Info.Price) {//提示不能购买

        } else {//弹窗提示是否购买

        }

    }

}


